﻿Imports System.Data.SqlClient

Public Class clsTarjetas
    Private mintIdTarjeta As Integer
    Private mstrCodigoTarjeta As String
    Private mintIdOperador As Integer
    Private mdatInicioVigencia As Date
    Private mdatTerminoVigencia As Date
    Private mintStatus As Integer
    Private mstrCodigo As String


    Public Property IdTarjeta() As Integer
        Get
            Return mintIdTarjeta
        End Get
        Set(ByVal value As Integer)
            mintIdTarjeta = value
        End Set
    End Property
    Public Property CodigoTarjeta() As String
        Get
            Return mstrCodigoTarjeta
        End Get
        Set(ByVal value As String)
            mstrCodigoTarjeta = value
        End Set
    End Property
    Public Property IdOperador() As Integer
        Get
            Return mintIdOperador
        End Get
        Set(ByVal value As Integer)
            mintIdOperador = value
        End Set
    End Property
    Public Property InicioVigencia() As Date
        Get
            Return mdatInicioVigencia
        End Get
        Set(ByVal value As Date)
            mdatInicioVigencia = value

        End Set
    End Property
    Public Property TerminoVigencia() As Date
        Get
            Return mdatTerminoVigencia
        End Get
        Set(ByVal value As Date)
            mdatTerminoVigencia = value
        End Set
    End Property
    Public Property Status() As Integer
        Get
            Return mintStatus
        End Get
        Set(ByVal value As Integer)
            mintStatus = value
        End Set
    End Property
    Public Property Codigo() As String
        Get
            Return mstrCodigo
        End Get
        Set(ByVal value As String)
            mstrCodigo = value
        End Set
    End Property
    Public Sub New()
        mintIdTarjeta = 0
        mstrCodigoTarjeta = ""
        mintIdOperador = 0
        mdatInicioVigencia = Now()
        mdatTerminoVigencia = Now()
        mintStatus = 0
        mstrCodigo = ""
    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Tarjetas (" & _
                            "CodigoTarjeta," & _
                            "IdOperador," & _
                            "InicioVigencia," & _
                            "TerminoVigencia," & _
                            "Status," & _
                            "Codigo)" & _
                            "VALUES (" & _
                            "substring(@CodigoTarjeta,1,10)," & _
                            "@IdOperador," & _
                            "@InicioVigencia," & _
                            "@TerminoVigencia," & _
                            "@Status," & _
                            "@Codigo)"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("CodigoTarjeta", mstrCodigoTarjeta)
        Command.Parameters.AddWithValue("IdOperador", mintIdOperador)
        Command.Parameters.AddWithValue("InicioVigencia", mdatInicioVigencia)
        Command.Parameters.AddWithValue("TerminoVigencia", mdatTerminoVigencia)
        Command.Parameters.AddWithValue("Status", mintStatus)
        Command.Parameters.AddWithValue("Codigo", mstrCodigo)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el registro ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update Tarjetas Set " & _
                    "CodigoTarjeta=@CodigoTarjeta " & _
                    "Where IdTarjeta=@IdTarjeta"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("IdTarjeta", mintIdTarjeta)
        Command.Parameters.AddWithValue("CodigoTarjeta", mstrCodigoTarjeta)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar la tarjeta ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
